package top.scsoul.voes.common.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class ExcelExportDownloadUtil {
    public static void excelExport(
            HttpServletResponse response,
            List<?> list,
            Class<?> clazz,
            String fileName) throws IOException {
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(StandardCharsets.UTF_8)));
        ExportParams exportParams = new ExportParams(fileName, fileName, ExcelType.XSSF);
        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, clazz, list);
        sheets.write(response.getOutputStream());
    }

    public static void excelExport(
            HttpServletResponse response,
            List<?> list,
            Class<?> clazz) throws IOException {
        excelExport(response, list, clazz, "表格文件");
    }
}
